package com.zl.tby.controller;

import com.zl.tby.core.common.R;
import com.zl.tby.dto.TbyUavRecordQuery;
import com.zl.tby.model.TbyUavRecord;
import com.zl.tby.service.ITbyUavRecordService;
import com.zl.tby.vo.TbyUavRecordForm;
import org.noear.solon.annotation.Body;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.core.handle.MethodType;
import org.noear.solon.validation.annotation.NotNull;
import org.noear.solon.validation.annotation.Valid;
import org.noear.solon.validation.annotation.Validated;

import java.util.List;

/**
 * 无人机使用记录 控制器
 * @author changpeng
 * @version 1.0
 * @since 2024/8/6 20:22
 */
@Valid
@Controller
@Mapping("/uav_record")
public class TbyUavRecordController {

    @Inject
    private ITbyUavRecordService tbyUavRecordService;

    /**
     * 保存或新增
     * @param form: 表单对象
     * @return 操作结果
     */
    @Mapping(value = "/save", method = MethodType.POST)
    public R<TbyUavRecord> saveOrUpdate(@Validated TbyUavRecordForm form) {
        try {
            if (form.getId() == null) {
                return tbyUavRecordService.add(form);
            }
            return tbyUavRecordService.update(form);
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.failed(ex.getMessage());
        }
    }

    /**
     * 删除数据
     * @param id: 主键id
     * @return 结果
     */
    @Mapping(value = "/delete", method = MethodType.DELETE)
    public R<Void> delete(@NotNull Long id) {
        return tbyUavRecordService.delete(id) == 1 ? R.ok() : R.failed("删除失败");
    }

    /**
     * 根据id查询
     * @param id: 主键id
     * @return 结果
     */
    @Mapping(value = "/get", method = MethodType.GET)
    public R<TbyUavRecordForm> get(@NotNull Long id) {
        TbyUavRecord entity = this.tbyUavRecordService.get(id);
        return entity == null ? R.failed("数据不存在") : R.ok(TbyUavRecordForm.recordConvert(entity, null, "yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * 分页查询
     * @param query: 查询参数
     * @return 分页结果
     */
    @Mapping(value = "/page", method = MethodType.GET)
    public R<?> page(@Body TbyUavRecordQuery query) {
        return R.ok(this.tbyUavRecordService.page(query));
    }

    /**
     * 列表查询
     * @param query: 查询参数
     * @return 列表结果
     */
    @Mapping(value = "/list", method = MethodType.GET)
    public R<List<TbyUavRecord>> List(@Body TbyUavRecordQuery query) {
        return R.ok(this.tbyUavRecordService.list(query));
    }

}
